//
//  ViewController.swift
//  ZSceneDemo
//
//  Created by 张鑫 on 2020/8/25.
//  Copyright © 2020 www.lionaitech.com. All rights reserved.
//

import UIKit

@_exported import SceneKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    
    enum FuncTitles: String {
        case Test
        case SunSystem
        case Geometry = "几何体"
        case Skeleton = "骨架动画"
        case Constraint = "约束"
        case SceneChange = "场景切换"
        case GeometrySource = "自定义"
        case Metal
        case def
        
        static private let titles = [FuncTitles.Test,
                                     FuncTitles.SunSystem,
                                     FuncTitles.Geometry,
                                     FuncTitles.Skeleton,
                                     FuncTitles.Constraint,
                                     FuncTitles.SceneChange,
                                     FuncTitles.GeometrySource,
                                     FuncTitles.Metal,
        ]
        
        static func title(index: Int) -> FuncTitles {
            return titles[index]
        }
        
        static var count: Int {
            return titles.count
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        // Do any additional setup after loading the view.
        
        let layout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        layout.itemSize = CGSize(width: 100, height: 50)
        
        let collection = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collection.translatesAutoresizingMaskIntoConstraints = false
        collection.backgroundColor = .clear
        collection.delegate = self
        collection.dataSource = self
        collection.register(CollectionFuncCell.self, forCellWithReuseIdentifier: "cell_id")
        view.addSubview(collection)
        
        collection.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        collection.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        collection.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        collection.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return FuncTitles.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell_id", for: indexPath) as? CollectionFuncCell else {
            return UICollectionViewCell()
        }
        cell.title = FuncTitles.title(index: indexPath.row).rawValue
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        switch FuncTitles.title(index: indexPath.row) {
        case .Test:
            navigationController?.pushViewController(Model3DViewController(), animated: true)
            
        case .SunSystem:
            navigationController?.pushViewController(SunSystemViewController(), animated: true)
            
        case .Geometry:
            navigationController?.pushViewController(GeometryViewController(), animated: true)
            
        case .Skeleton:
            navigationController?.pushViewController(SkeletonAnimationViewController(), animated: true)
            
        case .Constraint:
            navigationController?.pushViewController(ConstraintViewController(), animated: true)
            
        case .SceneChange:
            navigationController?.pushViewController(SceneChangeViewController(), animated: true)
            
        case .GeometrySource:
            navigationController?.pushViewController(GeometrySourceViewController(), animated: true)
            
        case .Metal:
            navigationController?.pushViewController(MetalViewController(), animated: true)
        default:
            print("nothing")
        }
    }
    
}


class CollectionFuncCell: UICollectionViewCell {
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.numberOfLines = 2
        label.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(label)
        
        label.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
        label.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
        label.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
        label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.backgroundColor = .lightGray
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var title: String = "" {
        didSet {
            titleLabel.text = title
        }
    }
    
}
